package com.yycx.module.bus.provider.handler.process.task;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.BusDrugs;
import com.yycx.module.bus.client.entity.BusOrder;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.provider.service.BusDrugsService;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusOrderService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单审批任务列表
 *
 * @author zyf
 */
@Component
public class OrderTaskTodoListHandler implements FlowPageInterceptor<TaskResponse> {

    @Resource
    private BusOrderService orderService;
    @Resource
    private BusDrugsService drugsService;

    @Resource
    private BusOrderDetailsService orderDetailsService;

    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String orderNo = params.get("orderNo");
        String companyId = OpenHelper.getCompanyId().toString();
        BusOrder order = orderService.getByProperty("orderNo",'%'+ orderNo);
        if (FlymeUtils.isNotEmpty(order)) {
            taskQuery.processInstanceBusinessKey(order.getOrderId().toString());
        } else {
            if (FlymeUtils.isNotEmpty(orderNo)) {
                taskQuery.processVariableValueLike("orderNo", orderNo);
            }
        }
        //关联租户ID
        taskQuery.taskTenantId(companyId);
        //关联流程key
        taskQuery.processDefinitionKey("process_order_" + companyId);
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = new EntityMap();
                String orderId = taskResponse.getBusinessKey();
                //查询订单信息
                if (FlymeUtils.isNotEmpty(orderId)) {
                    EntityMap orderMap = orderService.getMapByOrderId(Long.parseLong(orderId));
                    if (FlymeUtils.isNotEmpty(orderMap)) {
                        List<BusOrderDetails> orderDetailsList = orderDetailsService.selectByOrderId(Long.parseLong(orderId));
                        if (FlymeUtils.isNotEmpty(orderDetailsList)) {
                            BusOrderDetails orderDetails = orderDetailsList.get(0);
                            Long drugId = orderDetails.getDrugsId();
                            BusDrugs busDrugs = drugsService.getById(drugId);
                            orderMap.put("drugsSpec", busDrugs.getDrugsSpec());
                            orderMap.put("saleNum", orderDetails.getSaleNum());
                            orderMap.put("saleAmount", orderDetails.getSaleAmount());
                        }
                        map.putAll(orderMap);
                    }
                    map.put("orderId", taskResponse.getBusinessKey());
                    map.put("assignee", taskResponse.getAssignee());
                    map.put("processInstanceId", taskResponse.getProcessInstanceId());
                    map.put("taskName", taskResponse.getName());
                    map.put("taskId", taskResponse.getId());
                    map.put("endTime", taskResponse.getEndTime());
                    map.put("owner", taskResponse.getOwner());
                    map.put("createTime", taskResponse.getCreateTime());
                    list.add(map);
                }
            }
        }
        return list;
    }
}
